//
//  LBTabBar.m
//  LBWeibo
//
//  Created by lin on 15/7/6.
//  Copyright (c) 2015年 lin. All rights reserved.
//

#import "LBTabBar.h"
#import "UIView+Extension.h"
#import "LBTabBarBtn.h"
#define margin 5
#define btnWidth (self.width-2*margin)/5.0
#define COLOR_RANDOM  [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1]

@interface LBTabBar()
@property(nonatomic,weak)UIButton* plusBtn;
@end
@implementation LBTabBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    self.backgroundImage = [UIImage imageNamed:@"tabbar_background"];
    [self setUpPlusBtn];
    return self;
}

-(void)setUpPlusBtn{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.plusBtn = btn;
    [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    btn.size = [UIImage imageNamed:@"tabbar_compose_button"].size;
    [btn addTarget:self action:@selector(plusBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];
}


-(void)plusBtnClick:(UIButton*)plusBtn{
    [self.delegate tabBar:self didClickPlusBtn:plusBtn];
}



-(void)layoutSubviews{
    [super layoutSubviews];
    // 1.设置加号按钮的位置
    self.plusBtn.centerX = self.width * 0.5;
    self.plusBtn.centerY = self.height * 0.5;
    
    // 2.设置其他tabbarButton的位置和尺寸
    CGFloat tabbarButtonW = self.width / 5;
    CGFloat tabbarButtonIndex = 0;
    for (UIView *child in self.subviews) {
        Class class = NSClassFromString(@"UITabBarButton");
        if ([child isKindOfClass:class]) {
            // 设置宽度
            child.width = tabbarButtonW;
            // 设置x
            child.x = tabbarButtonIndex * tabbarButtonW;
            
            // 增加索引
            tabbarButtonIndex++;
            if (tabbarButtonIndex == 2) {
                tabbarButtonIndex++;
            }
        }
    }
}



@end
